"use strict";
//实现TodoList类
class TodoList {
    constructor() {
        this.todos = [];
    }
    addTodo(title) {
        const newTodo = {
            id: Date.now(),
            title,
            completed: false,
        };
        this.todos.push(newTodo);
    }
    removeTodo(id) {
        this.todos = this.todos.filter(todo => todo.id !== id);
    }
    toComplete(id) {
        const todo = this.todos.find(todo => todo.id === id);
        if (todo) {
            todo.completed = !todo.completed;
        }
    }
}
//处理dom操作
const todoList = new TodoList();
const inputElement = document.getElementById('new-todo');
const addButton = document.getElementById('add-todo');
const listElement = document.getElementById('todo-list');
addButton.addEventListener('click', () => {
    if (inputElement.value.trim()) {
        todoList.addTodo(inputElement.value);
        inputElement.value = '';
        renderTodos();
    }
});
//渲染任务列表
function renderTodos() {
    listElement.innerHTML = '';
    todoList.todos.forEach(todo => {
        const li = document.createElement('li');
        li.textContent = todo.title;
        const deleteButton = document.createElement('button');
        deleteButton.textContent = '删除';
        deleteButton.addEventListener('click', () => {
            todoList.removeTodo(todo.id);
            renderTodos();
        });
        li.appendChild(deleteButton);
        listElement.appendChild(li);
    });
}
